package org.pentaho.di.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class PersistenceDataUtil {
	
	
	private static final 	String  URL=System.getProperty("user.dir")+System.getProperty("file.separator")+"sip_temp.dat";
	

	public Object getData() {
		File file = new File(this.URL);
		ObjectInputStream reader = null;
		Object obj = null;
		if (file.exists()) {
			try {
				reader = new ObjectInputStream(new FileInputStream(file));
				obj = reader.readObject();
			} catch (Exception e) {
				throw new RuntimeException(e);
			} finally {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}
		return obj;
	}
	
	public <T extends Serializable> void saveData(final T obj) {
		ObjectOutputStream writer = null;
		File file = new File(this.URL);
			try {
				writer = new ObjectOutputStream(new FileOutputStream(file));
				writer.writeObject(obj);
			} catch (IOException e) {
				throw new RuntimeException(e);
			} finally {
				try {
					writer.close();
				} catch (IOException e) {
				}
			}
	}
	public static void main(String[] args) {

/*		System.err.println(URL);
		PersistenceDataUtil test = new PersistenceDataUtil();
		HashMap<String, Integer> mp = new HashMap<String, Integer>();
		mp.put("period", new Integer(50));
		test.saveData(mp);
		Map map = (Map) test.getData();
		System.err.println(map.get("period"));*/
	}	
}
